import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import dts from 'vite-plugin-dts'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const isLib = mode === 'lib'
  
  return {
    plugins: [
      vue({
        script: {
          defineModel: true,
          propsDestructure: true
        }
      }),
      ...(isLib ? [
        dts({
          include: ['src/**/*'],
          exclude: ['src/**/__tests__/*'],
          outDir: 'dist',
          staticImport: true,
          insertTypesEntry: true,
        })
      ] : [])
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },
    build: isLib ? {
      lib: {
        entry: resolve(__dirname, 'src/index.ts'),
        name: 'CompanyUI',
        formats: ['es', 'umd'],
        fileName: (format) => `index.${format}.js`
      },
      rollupOptions: {
        external: ['vue'],
        output: {
          globals: {
            vue: 'Vue'
          }
        }
      },
      cssCodeSplit: false
    } : {
      outDir: 'examples/dist'
    },
    server: {
      port: 3000,
      open: true
    }
  }
})
